//
//  ByViewMinePointHeader.m
//  chequanbao_ios
//
//  Created by mc on 2017/5/16.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByViewMinePointHeader.h"

// vc
//#import "ByUIWithdrawVC.h"
#import "ByUIWithdrawNewVC.h"

@implementation ByViewMinePointHeader
{
    NSInteger _prevTag;
    UIButton *_selectBtn;
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self createViews];
    }
    return self;
}



//-(void)setPoints:(NSString *)points{
//    _points = points;
//    [self createViews];
//
//}
#pragma mark -- 创建视图
- (void)createViews{
    // 积分
    CGFloat x,y,width,height;
//    NSString *str = [NSString stringWithFormat:@"我的车险金：%@积分",_points];
//    width = [str getSizeOfString:16].width;
//    height = 20;
//    x = (SCREEN_WIDTH - width)/2.0 + 25;
//    y = 10;
//    UILabel *pointLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
//    pointLabel.text = str;
//    pointLabel.font = FONT(16);
//    pointLabel.textColor = [UIColor whiteColor];
////    pointLabel.backgroundColor = [UIColor yellowColor];
//    [self addSubview:pointLabel];
    
    // 图标
//    x = CGRectGetMinX(pointLabel.frame) - 25;
//    y = y ;
//    width = height = 20;
//    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
//    img.image = [UIImage imageNamed:@"mine_small_points"];
//    [self addSubview:img];
    
    
    // 提现
    width = [@"提现" getSizeOfString:14].width + 80;
    x = (SCREEN_WIDTH - width)/2;
    y = 10;
    height = 30;
    UIButton *withdrawalBtn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
    [withdrawalBtn setTitle:@"提现" forState:UIControlStateNormal];
    withdrawalBtn.titleLabel.font = FONT(14);
    withdrawalBtn.backgroundColor = BY_THEME_COLOR;
    withdrawalBtn.layer.cornerRadius = height/2.0;
    [withdrawalBtn addTarget:self action:@selector(withdrawAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:withdrawalBtn];
    
    
    // 我的收入与支出
    NSArray *titles = @[@"我的收入", @"我的支出"];
    width = (self.width - 20) / titles.count;
    height = 40;
    y = withdrawalBtn.bottom + 20;
    for (int i = 0; i < titles.count; i ++) {
        x = i * width + 10;
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
        [button setTitle:titles[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        button.titleLabel.font = B_FONT(18);
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        button.tag = 10 + i;
        
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        if (button.tag == 10) {
            _prevTag = button.tag;
            _selectBtn = button;
            UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:button.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft cornerRadii:CGSizeMake(20, 20)];
            maskLayer.path = maskPath.CGPath;
            button.backgroundColor = [UIColor colorWithWhite:1 alpha:0.25];

        }else{
            UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:button.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerBottomRight cornerRadii:CGSizeMake(20, 20)];
            maskLayer.path = maskPath.CGPath;
            button.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.25];

        }
        maskLayer.frame = button.bounds;
        button.layer.mask = maskLayer;
        
    
        
    }

}

#pragma mark - 点击事件
- (void)buttonAction:(UIButton *)button {
    
    // 点击了已经选中的按钮
    if (button.tag == _prevTag) {
        return;
    }
    button.selected = !button.selected;
    // 取消上个选中
    UIButton *btn = [self viewWithTag:_prevTag];
    btn.selected = NO;
    btn.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.25];
    
    // 更改上个被选中的索引
    _prevTag = button.tag;
    
    if (button.selected) {
        button.backgroundColor = [UIColor colorWithWhite:1 alpha:0.25];
    }else{
        button.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.25];
    }
    
    if (self.selectBlock) {
        self.selectBlock(button.tag - 10);
    }

}

// 提现
- (void)withdrawAction{
    ByUIWithdrawNewVC *vc = [[ByUIWithdrawNewVC alloc] init];
    [(ByBaseVC *)self.viewController by_normalPush:vc hasNavi:YES];

}

@end
